﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using NguyenHoa.Web.App_Code;
using NguyenHoa.Domains;
using NguyenHoa.Services;
using System.IO;
using DevExpress.Web.ASPxDataView;
using System.Web.UI.HtmlControls;


namespace NguyenHoa.Web.Pages
{
  public partial class NewsLetterPage : BasePage
  {
    private int ERR0001 = -2146232060;

    internal override string PageTitle
    {
      get
      {
        return Resources.Strings.Newsletter;
      }
    }

    #region Events
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void ctrlCallback_Callback(object source, DevExpress.Web.ASPxCallback.CallbackEventArgs e)
    {
      if (e.Parameter.StartsWith("save"))
      {
        e.Result = Insert();
      }
    }
    #endregion

    #region Methods

    private string Insert()
    {
      if (!Utils.ValidateEmail(txtEmail.Text))
      {
        return Resources.Messages.ERR0059;
      }
      try
      {
        NewsLetter nw = new NewsLetter();
        nw.Email = txtEmail.Text;
        nw.CustomerName = txtName.Text.Trim();
        nw.Note = mmNote.Text.Trim();
        new NewsLetterService().Save(nw);
      }
      catch (System.Data.SqlClient.SqlException ex)
      {
        if (ex.ErrorCode == ERR0001)
        {
          WriteLog("NewsLetterPage.aspx::Save NewsLetter", ex);
          return string.Format(Resources.Messages.ERR0061, "Email");
        }
        else
        {
          throw new Exception(ex.Message, ex);
        }
      }
      catch (Exception ex)
      {
        WriteLog("NewsLetterPage.aspx::Save NewsLetter", ex);
        return Resources.Messages.ERR0054;
      }
      return string.Empty;
    }

    #endregion
  }
}
